package com.chenzehe.alimama.html;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.chenzehe.alimama.domain.Product;
import com.chenzehe.alimama.domain.TaoBaoImg;
import com.chenzehe.java.core.Status;

/**
 * 
 * @description 列表页
 * @author chenzehe
 * @email hljuczh@163.com
 * 
 *        2013-11-27 下午11:25:31
 */
public abstract class ListPage extends BasePage {

	public String pageUrlTemplate;
	public List<WebElement> listWebElements;
	public List<Product> listProducts;
	public String username;
	// 获得s.click的URL
	String allCodeUrlTemplate = "http://u.alimama.com/union/spread/common/allCode.htm?specialType=taokequn&auction_id=${auctionid}&group_id=${groupid}";
	
	public ListPage(WebDriver webDriver, String pageUrlTemplate, String username) {
		super(webDriver);
		this.pageUrlTemplate = pageUrlTemplate;
		this.username = username;
	}

	/**
	 * 取得当前页面的产品数据
	 * 
	 * @param pageNo
	 * @return
	 */
	public List<Product> getProductList(int pageNo) {
		navigatePageNo(pageNo);
		setListWebElements();
		initListProducts();
		return listProducts;
	}

	/**
	 * 设置列表元素
	 * 
	 * @return
	 */
	public void setListWebElements() {
		this.listWebElements = getWebDriver()
				.findElement(By.id("J_listMainTable"))
				.findElements(By.tagName("tbody")).get(0)
				.findElements(By.tagName("tr"));
	}

	/**
	 * 初始化本页产品到listProducts
	 */
	public void initListProducts() {
		listProducts = new ArrayList<Product>();
		for (WebElement tr : listWebElements) {
			Product product = new Product();
			product.setTaoId(initTaoId(tr));
			product.setImg(initImg(tr));
			product.setTitle(initTitle(tr));
			product.setPrice(initPrice(tr));
			product.setGoUrl(initAllCodeUrl(tr));// 先得到allCode.htm的URL，下面再根据这些URL获取s.click链接
			product.setUserName(this.username);
			product.setIssueTime(new Date());
			product.setStatus(Status.STATUS_NORMAL.getState());
			listProducts.add(product);
		}
		for (Product product : listProducts) {
			// 把allCode.htm的链接转化成s.click链接
			product.setGoUrl(initGoUrl(product.getGoUrl()));
			// log.info(product.getGoUrl());
		}
	}

	/**
	 * 初始化获取淘宝客链接
	 */
	public String initAllCodeUrl(WebElement tr) {		
		// 得到立即推广按钮
		WebElement a = tr.findElement(By.className("use-now")).findElement(
				By.tagName("a"));
		String auctionid = a.getAttribute("auctionid");
		String groupid = a.getAttribute("groupid");
		String allCodeUrl = allCodeUrlTemplate.replace("${auctionid}", auctionid).replace(
				"${groupid}", groupid);
		return allCodeUrl;
	}

	/**
	 * 分析出价格，把单位去掉
	 */
	public float initPrice(WebElement tr) {
		return Float.valueOf(getPriceElement(tr).getText().replace("元", ""));
	}

	/**
	 * 分析出标题
	 */
	public String initTitle(WebElement tr) {
		return getTitleElement(tr).getText();
	}

	/**
	 * 分析出taobao detail id
	 */
	public long initTaoId(WebElement tr) {
		String taoHref = getUrlElement(tr).getAttribute("href");
		String[] strs = taoHref.split("id=");
		if (strs.length > 1) {
			return Long.valueOf(strs[1]);
		}
		return 0;
	}

	/**
	 * 取得图片URL，并把後面_sum.jpg去掉，保存原圖URL
	 */
	public String initImg(WebElement tr) {
		String url = getImgElement(tr).getAttribute("src");
		return new TaoBaoImg(url).getUrl();
	}

	/**
	 * 取得淘宝客链接
	 */
	public String initGoUrl(String codeurl) {
		navigate(codeurl);
		String goUrl = getWebDriver().findElement(By.id("J_codeArea"))
				.getText();
		return goUrl;
	}

	/**
	 * 取得链接标签
	 */
	public WebElement getUrlElement(WebElement tr) {
		return tr.findElements(By.tagName("a")).get(0);
	}

	/**
	 * 取得价格标签
	 */
	public WebElement getPriceElement(WebElement tr) {
		return tr.findElements(By.tagName("td")).get(2);
	}

	/**
	 * 取得标题标签
	 */
	public WebElement getTitleElement(WebElement tr) {
		return tr.findElements(By.tagName("a")).get(1);
	}

	/**
	 * 取得图片标签
	 */
	public WebElement getImgElement(WebElement tr) {
		return tr.findElement(By.tagName("img"));
	}

	/**
	 * 跳转到pageNo
	 */
	public void navigatePageNo(int pageNo) {
		// 把${pageNo}替换成pageNo
		String goPageUrl = pageUrlTemplate.replace("${pageNo}", String.valueOf(pageNo));
		System.out.println("goPageUrl:" + goPageUrl);
		navigate(goPageUrl);
	}

}
